﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excell_On_Services.Models;
using Excell_On_Services.ViewModels;
using System.ComponentModel;
using Excell_On_Services.Infrastructure.Config;

namespace Excell_On_Services.Controllers
{
    public class ServiceController : Controller
    {
        public IEOSRepository Repository { get; set; }

        public ServiceController() :this (new EFEOSRepository())
        {
        }

        public ServiceController(IEOSRepository repository)
        {
            Repository = repository;
        }

        //
        // GET: /Service/
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Index([DefaultValue(0)] int pageIndex)
        {
            //int departmentsPerPage = Configuration.Instance.PostPerPage;
            int servicesPerPage = 4;
            return ShowServices(
                () => Repository.FindServicesInPage(pageIndex, servicesPerPage),
                () => Repository.GetServiceCount(),
                pageIndex, "Index", servicesPerPage
            );

        }
        [Authorize(Roles = "Manager,Employee,Administrators")]
        private ActionResult ShowServices(Func<IEnumerable<Service>> filterFunc, Func<int> countFunc, int pageIndex, string actionName, int servicePerPage)
        {
            var services = filterFunc();
            var serviceCount = countFunc();
            var model = new ServicesDisplayModel
            {
                Services = services,
                PagerModel = new PagerModel
                {
                    CurrentPageIndex = pageIndex,
                    ItemsPerPage = servicePerPage,
                    TotalItems = serviceCount,
                    PageUrlTemplate = Url.Action(actionName, new { pageIndex = PagerModel.PageSymbol })
                }
            };
            return View("Index", model);
        }

        //
        // GET: /Service/Details/5

        public ActionResult Details(int id)
        {
            var service = Repository.FindServiceById(id);
            //var model = new ServiceModel
            //{
            //    ServiceID = id,
            //    Name = service.Name,
            //    Price = service.Price,
            //    Description = service.Description,
            //    ParentID = service.ParentID
            //};
            return View(service);
        }

        //
        // GET: /Service/Create
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Create()
        {
            var services = Repository.FindAllServices();
            ViewData["ServiceType"] = new SelectList(services, "ServiceID", "Name");
            return View();
        } 

        //
        // POST: /Service/Create

        [HttpPost]
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Create(ServiceModel model)
        {
            try
            {
                // TODO: Add insert logic here

                if (!ModelState.IsValid)
                {
                    var services = Repository.FindAllServices();
                    ViewData["ServiceType"] = new SelectList(services, "ServiceID", "Name");
                    return View(model);
                }
                var service = new Service
                {
                    Price = model.Price,
                    Description = model.Description,
                    Name = model.Name,
                    ParentID = model.ParentID
                };
                Repository.AddService(service);
                Repository.CommitChanges();
                return RedirectToAction("Details", new { id = service.ServiceID });
            }
            catch
            {
                var services = Repository.FindAllServices();
                ViewData["ServiceType"] = new SelectList(services, "ServiceID", "Name");
                return View();
            }
        }
        
        //
        // GET: /Service/Edit/5
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Edit(int id)
        {
            var services = Repository.FindAllServicesExpertID(id);
            ViewData["ServiceType"] = new SelectList(services, "ServiceID", "Name");

            var service = Repository.FindServiceById(id);
            var model = new ServiceModel
            {
                ServiceID = service.ServiceID,
                Name = service.Name,
                Description = service.Description,
                Price = service.Price,
                ParentID = service.ParentID
            };
            return View(model);
        }

        //
        // POST: /Service/Edit/5

        [HttpPost]
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Edit(int id, ServiceModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var services = Repository.FindAllServicesExpertID(id);
                    ViewData["ServiceType"] = new SelectList(services, "ServiceID", "Name");
                    return View(model);
                }

                var item = Repository.FindServiceById(model.ServiceID.Value);
                item.Name = model.Name;
                item.Description = model.Description;
                item.Price = model.Price;
                item.ParentID = model.ParentID;
                Repository.CommitChanges();
                return RedirectToAction("Details", new { id = item.ServiceID });
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Service/Delete/5
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Service/Delete/5

        [HttpPost]
        [Authorize(Roles = "Manager,Employee,Administrators")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
